import axios from 'axios';
import Cookies from "js-cookie";
let Base64 = require('js-base64').Base64;
//import store from '../store';
// import router from '../router';

// 创建axios实例
const service = axios.create({
  baseURL: process.env.BASE_API, // api的base_url
  timeout: 50000                  // 请求超时时间
});

// request拦截器
service.interceptors.request.use(config => {
//	console.log(Base64.encode("1"));
  // Do something before request is sent
  var token = Cookies.get('token');
  var time = Math.round(new Date().getTime()/1000);
  

	if (token) {
	    config.headers['token'] =Base64.encode(token+','+time); // 让每个请求携带token--['X-Token']为自定义key 请根据实际情况自行修改
	    config.headers['Content-Type']='application/json';
	    
	}
//	console.log(Base64.encode(token+','+time))
//	console.log(time)
  return config;
}, error => {
  // Do something with request error
  console.log(error); // for debug
  Promise.reject(error);
})

// respone拦截器
service.interceptors.response.use(
 response =>{
 	if(response.data.code==200){
 		 return response;
 	}
 	else if (response.data.code ==445||response.data.code ==410||response.data.code ==401||response.data.code==400) {
//	 	 Message({
//		   message: response.data.msg,
//		   type: 'error',
//		   duration: 5 * 1000
//		 });
		 return Promise.reject(response);
	 }
 	else if(response.data.code==251||response.data.code==252){
 		return Promise.reject(response);
 	}
	else{
//	 	  Message({
//		   message: response.data.msg,
//		   type: 'error',
//		   duration: 5 * 1000
//		 });
		 return Promise.reject(response);
	 }
 },
  /**
  * 下面的注释为通过response自定义code来标示请求状态，当code返回如下情况为权限有问题，登出并返回到登录页
  * 如通过xmlhttprequest 状态码标识 逻辑可写在下面error中
  */

  error => {
    console.log('err' + error);// for debug
//  Message({
//    message: error.message,
//    type: 'error',
//    duration: 5 * 1000
//  });
    return Promise.reject(error);
  }
)

export default service;
